package com.service.sys;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.common.StringUtils;
import com.common.http.HttpResult;
import com.common.model.Global;
import com.dao.sys.SysDictMapper;
import com.model.sys.SysDict;

/**
 * @author lijiazhen
 * @date 2022-02-22 param description
 */
@Service
public class SysDictService {

	@Autowired
	private SysDictMapper sysDictMapper;

	public String selectTreeList(String dicId) {
		if (StringUtils.isNullOrEmpty(dicId)) {
			dicId = "0";
		}
		HashMap<String, Object> hashMap = new HashMap<String, Object>();
		hashMap.put("parentId", dicId);
		List<SysDict> list = sysDictMapper.selectDictListByParams(hashMap);
		List<SysDict> allList = sysDictMapper.pageList();
		Map<String, SysDict> map = allList.stream()
				.collect(Collectors.toMap(SysDict::getDicParentId, Function.identity(), (u1, u2) -> u1));
		for (SysDict sysDict : list) {
			if (map.containsKey(sysDict.getDicId())) {
				sysDict.setState("closed");
			} else {
				sysDict.setState("open");
			}
		}
		String jsonString = JSON.toJSONString(list);
		return jsonString;
	}

	public Object delete(int id) {
		int ret = sysDictMapper.delete(id);
		return null;
	}

	public Object update(SysDict sysDict) {
		int ret = sysDictMapper.update(sysDict);
		return null;
	}

	public SysDict load(int id) {
		return sysDictMapper.load(id);
	}

	public List<SysDict> selectDictListByParams(Map<String, Object> params) {
		return sysDictMapper.selectDictListByParams(params);
	}

	/*
	 * public Map<String,Object> pageList(int offset, int pagesize) {
	 * 
	 * List<SysDict> pageList = sysDictMapper.pageList(); int totalCount =
	 * sysDictMapper.pageListCount(offset, pagesize);
	 * 
	 * // result Map<String, Object> result = new HashMap<String, Object>();
	 * result.put("pageList", pageList); result.put("totalCount", totalCount);
	 * 
	 * return result; }
	 */
	public List queryItemByTypeName(String dicName) {
		HttpResult httpResult = new HttpResult();
		List<SysDict> result = new ArrayList<>();
		Map param = new HashMap();
		param.put("dicName", dicName);
		param.put("status", Global.YES);
		List<SysDict> dicList = selectDictListByParams(param);
		if (dicList != null && dicList.size() > 0) {
			SysDict sysDict = dicList.get(0);
			param.clear();
			param.put("parentId", sysDict.getDicId());
			param.put("status", Global.YES);
			/*
			 * if (StringUtils.isNoneBlank(q)) { param.put("query", q); }
			 */

			result = selectDictListByParams(param);
		}
		httpResult.setRows(result);
		return result;
	}

	public List queryItemByTypeName(Map queryParams) {
		String dicName = (String) queryParams.get("dicName");
		String query = (String) queryParams.get("q");
		HttpResult httpResult = new HttpResult();
		List<SysDict> result = new ArrayList<>();
		Map param = new HashMap();
		param.put("dicName", dicName);
		if (queryParams.get("pageRows") != null) {
			param.put("pageRows", Integer.parseInt(queryParams.get("pageRows").toString()));
			param.put("pageNum", Integer.parseInt(queryParams.get("pageNum").toString()));
		}
		List<SysDict> dicList = selectDictListByParams(param);
		if (dicList != null && dicList.size() > 0) {
			SysDict sysDict = dicList.get(0);
			param.clear();
			param.put("parentId", sysDict.getDicId());
			if (queryParams.get("pageRows") != null) {
				param.put("pageRows", Integer.parseInt(queryParams.get("pageRows").toString()));
				param.put("pageNum", Integer.parseInt(queryParams.get("pageNum").toString()));
			}
			if (StringUtils.isNotNull(query)) {
				param.put("query", query);
			}

			result = selectDictListByParams(param);
		}
		httpResult.setRows(result);
		return result;
	}

}
